package com.base.myproject.server;



import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.base.myproject.server.database.PoolMan;

public class TableSaver {
	String xml = null;
	SAXReader saxReader = new SAXReader();

	public TableSaver() {

	}

	public TableSaver(String xml) {
		this.xml = xml;
	}

	public String getXml() {
		return xml;
	}

	public void setXml(String xml) {
		this.xml = xml;
	}

	public boolean save() {
		Vector v = new Vector();
		try {
			Document document = DocumentHelper.parseText(xml);

			//取得表明
			 Element elementtable =  (Element)document.selectSingleNode("/root/table");
			 System.out.println(elementtable.getText());
			 String table = elementtable.getText();
			 //删除子表
				List deletechilelist = document.selectNodes("/root/deletechile");
				Iterator deletechileiterator = deletechilelist.iterator();
				
				while (deletechileiterator.hasNext()) {
					Element element = (Element) deletechileiterator.next();
				System.out.println("value--"+	element.getText());
				String[] arr  = element.getText().split(";");
				for(String s : arr)
				{
					if(s==null || s.equals(""))
					{
						
					}
					else
					{
						v.add(s);
					}
//					v.add(s);
					System.out.println(s);
				}
			
				}
			 //取得插入数据
			List insertlist = document.selectNodes("/root/inserts/insert");
			Iterator insertiterator = insertlist.iterator();
			while (insertiterator.hasNext()) {
				StringBuffer insertsql = new StringBuffer(" insert into "+table+ "  (");
				StringBuffer set = new StringBuffer();
				StringBuffer values = new StringBuffer();
				
				Element element = (Element) insertiterator.next();
				List<Element> setrecord = element.selectNodes("*");
				for(Element element2 :setrecord)
				{
					if(element2.getName().equals("set"))
					{
						//System.out.println(element2.getText()+"");
						set.append(element2.getText()+")");
					}
					else if(element2.getName().equals("values"))
					{
						//System.out.println(element2.getText()+"");
						values.append("values("+element2.getText()+")");
					}
				}
				insertsql.append(set);
				insertsql.append(values);
				v.add(insertsql.toString());
				System.out.println(insertsql);
				System.out.println(element.selectObject("table"));
			}
//			取得删除数据
			List deletelist = document.selectNodes("/root/deletes/delete");
			Iterator deleteiterator = deletelist.iterator();
			while (deleteiterator.hasNext()) {
				Element element = (Element) deleteiterator.next();
				List<Element> deleterecord = element.selectNodes("*");
				StringBuffer deletesql = new StringBuffer(" delete from "+table +" where ");
				for(Element deleteelement :deleterecord)
				{
					
					deletesql.append(deleteelement.getName()+" = '"+deleteelement.getText()+"' and ");
				}
				deletesql.delete(deletesql.length()-5, deletesql.length()-1);
				System.out.println(deletesql);
				v.add(deletesql.toString());
			}
			
			//取得更新数据
			List updatelist = document.selectNodes("/root/updates/update");
			Iterator updateiterator = updatelist.iterator();
			
			while (updateiterator.hasNext()) {
				Element element = (Element) updateiterator.next();
				List<Element> record = element.selectNodes("*");
				StringBuffer update = new StringBuffer(" update "+table+" set ");
				StringBuffer where = new StringBuffer("  where ");
				for(Element element2 :record)
				{
					if(element2.getName().equals("key"))
					{
						List<Element> keyrocord = element2.selectNodes("*");
						for(Element keyelement :keyrocord)
						{
							if(keyelement.getText().equals("null"))
							{
								where.append( keyelement.getName()+" is null and " ) ;
							}
							else
							{
							where.append( keyelement.getName()+" = '"+keyelement.getText() +"' and " ) ;
							}
							System.out.println(keyelement.getName()+"="+keyelement.getText());
						}
					}
					else
					{
					System.out.println(element2.getName()+"="+element2.getText());
					update.append(element2.getName() +" =  '"+element2.getText()+"'," );
					}
					
				}
				update.deleteCharAt(update.length()-1);
				where.delete(where.length()-5, where.length()-1);
				update.append(where);
			System.out.println("update="+update);
			v.add(update.toString());
			}
			
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		PoolMan pm = new PoolMan();
		
		try {
			pm.getPool("test");
			pm.handleTransaction(v);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
		
		}
		return false;
	}
public static void main(String[] argv)
{
	TableSaver ts = new TableSaver();
	ts.setXml("<?xml version=\"1.0\" encoding=\"UTF-8\"?><root><table>contract</table><inserts><insert><set><![CDATA[ccode,bcode,icode]]></set><values><![CDATA['ccode','zgw','3']]></values></insert></inserts><deletes><delete><icode>3</icode></delete></deletes><updates></updates></root>");
	ts.save();
}
}
